package util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import model.Instrument;

/**
 * A Factory Class used to load an store images used in the application.
 */
public final class Images {
	private Images() {}

//Application
	private static BufferedImage appIcon;
	public static Image getApplicationIcon() {
		if(appIcon == null) {
			try {
				appIcon = ImageIO.read(FileIO.getResource("/data/icon.png"));
			} catch(IOException ioe) {
				System.out.println("Failed to load Application Icon");
			}
		}
		return appIcon;
	}

	private static BufferedImage menuSheet;
	private static final int MENU_ICON_SIZE = 32;
	private static void loadMenuSheet() {
		if(menuSheet != null) return;
		try {
			menuSheet = ImageIO.read(FileIO.getResource("/data/menu.png"));
		} catch(IOException ioe) {
			System.out.println("Failed to load Menu Icons");
		}
	}

//Play
	private static BufferedImage playIcon;
	public static Image getPlayIcon(int size) {
		Images.loadMenuSheet();
		if(playIcon == null) {
			playIcon = menuSheet.getSubimage(0, 0, MENU_ICON_SIZE, MENU_ICON_SIZE);
		}
		return playIcon.getScaledInstance(size, size, Image.SCALE_SMOOTH);
	}

//Pause
	private static BufferedImage pauseIcon;
	public static Image getPauseIcon(int size) {
		Images.loadMenuSheet();
		if(pauseIcon == null) {
			pauseIcon = menuSheet.getSubimage(32, 0, MENU_ICON_SIZE, MENU_ICON_SIZE);
		}
		return pauseIcon.getScaledInstance(size, size, Image.SCALE_SMOOTH);
	}

//Stop
	private static BufferedImage stopIcon;
	public static Image getStopIcon(int size) {
		Images.loadMenuSheet();
		if(stopIcon == null) {
			stopIcon = menuSheet.getSubimage(64, 0, MENU_ICON_SIZE, MENU_ICON_SIZE);
		}
		return stopIcon.getScaledInstance(size, size, Image.SCALE_SMOOTH);
	}

//Options
	private static BufferedImage optionsIcon;
	public static Image getOptionsIcon(int size) {
		Images.loadMenuSheet();
		if(optionsIcon == null) {
			optionsIcon = menuSheet.getSubimage(96, 0, MENU_ICON_SIZE, MENU_ICON_SIZE);
		}
		return optionsIcon.getScaledInstance(size, size, Image.SCALE_SMOOTH);
	}

	private static Image[] instrumentIcons;
	public static Image getInstrumentIcon(Instrument instrument) {
		if(instrumentIcons == null) {
			Instrument[] types = Instrument.values();
			instrumentIcons = new Image[types.length];
			for(int i = 0; i < types.length; i++) {
				try {
					instrumentIcons[i] = ImageIO.read(
						FileIO.getResource("/data/icon/"+types[i].toString().toLowerCase()+".png")
					);
				} catch(IOException ioe) {
					System.out.println("Failed to load Instrument Icon " + types[i].toString());
					instrumentIcons[i] = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
				}
			}
		}
		return instrumentIcons[instrument.ordinal()];
	}

	private static final int OPTIONS_ICON_SIZE = 48;
	private static BufferedImage optionsSheet;
	private static void loadOptionsSheet() {
		if(optionsSheet != null) return;

		try {
			optionsSheet = ImageIO.read(FileIO.getResource("/data/options.png"));
		} catch(IOException ioe) {
			System.out.println("Failed to load Option Icons");
		}
	}

//Load Piece
	private static BufferedImage loadIcon;
	public static Image getLoadIcon() {
		Images.loadOptionsSheet();
		if(loadIcon == null) {
			loadIcon = optionsSheet.getSubimage(
				0, 0, OPTIONS_ICON_SIZE, OPTIONS_ICON_SIZE
			);
		}
		return loadIcon;
	}

//Save Piece
	private static BufferedImage saveIcon;
	public static Image getSaveIcon() {
		Images.loadOptionsSheet();
		if(saveIcon == null) {
			saveIcon = optionsSheet.getSubimage(
				OPTIONS_ICON_SIZE, 0, OPTIONS_ICON_SIZE, OPTIONS_ICON_SIZE
			);
		}
		return saveIcon;
	}

//Convert Piece
	private static BufferedImage convertIcon;

	public static Image getConvertIcon() {
		Images.loadOptionsSheet();
		if(convertIcon == null) {
			convertIcon = optionsSheet.getSubimage(
				OPTIONS_ICON_SIZE*3, 0, OPTIONS_ICON_SIZE, OPTIONS_ICON_SIZE
			);
		}
		return convertIcon;
	}

//New Piece
	private static BufferedImage newIcon;

	public static Image getNewIcon() {
		Images.loadOptionsSheet();
		if(newIcon == null) {
			newIcon = optionsSheet.getSubimage(
				OPTIONS_ICON_SIZE*2, 0, OPTIONS_ICON_SIZE, OPTIONS_ICON_SIZE
			);
		}
		return newIcon;
	}

//Note Marker
	private static BufferedImage[] noteMarker;
	public static Image getMarker(boolean activated) {
		if(noteMarker == null) {
			BufferedImage sheet = null;
			try {
				sheet = ImageIO.read(FileIO.getResource("/data/note.png"));
			} catch(IOException ioe) {
				System.out.println("Failed to load note sheet.");
			}
			noteMarker = new BufferedImage[]{
				sheet.getSubimage(0, 0, 16, 22),
				sheet.getSubimage(16, 0, 16, 22)
			};
		}

		return noteMarker[activated ? 1 : 0];
	}

//Delete Icon
	private static BufferedImage deleteIcon;
	public static Image getDeleteIcon() {
		if(deleteIcon == null) {
			try {
				deleteIcon = ImageIO.read(FileIO.getResource("/data/delete.png"));
			} catch(IOException ioe) {
				System.out.println("Failed to load Delete Icon");
			}
		}
		return deleteIcon;
	}

}
